package fun.cyhgraph.controller.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import fun.cyhgraph.annotation.AuthCheck;
import fun.cyhgraph.common.ErrorCode;
import fun.cyhgraph.common.UserConstant;
import fun.cyhgraph.common.UserRoleEnum;

import fun.cyhgraph.mapper.UserMapper;
import fun.cyhgraph.model.dto.*;
import fun.cyhgraph.model.entity.Employee;
import fun.cyhgraph.model.entity.Order;
import fun.cyhgraph.model.entity.User;
import fun.cyhgraph.exception.BusinessException;
import fun.cyhgraph.exception.ThrowUtils;
import fun.cyhgraph.properties.JwtProperties;
import fun.cyhgraph.result.PageResult;
import fun.cyhgraph.result.Result;
import fun.cyhgraph.service.EmployeeService;
import fun.cyhgraph.service.OrderService;
import fun.cyhgraph.service.UserService;
import fun.cyhgraph.model.vo.EmployeeLoginVO;
import fun.cyhgraph.model.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/common")
@Tag(name = "通用接口管理")
@Slf4j
public class CommonController {
    @Resource
    private EmployeeService employeeService;
    @Resource
    private UserService userService;
    @Resource
    private OrderService orderService;
    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public Result<Boolean> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.userLogout(request);
        if (!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "退登失败");
        }
        return Result.success("成功");
    }
    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @Operation(summary = "获取当前登录用户")
    @GetMapping("/get/login")
    public Result<UserLoginVO> getLoginUser(HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return Result.success(userService.getLoginUserVO(user));
    }

    /**
     * 更新用户
     * @param request
     * @return
     */
    @Operation(summary = "更新用户")
    @PostMapping("/update")
    public Result<Boolean> updateUser(@RequestBody UserUpdateDto userUpdateDto,
                                      HttpServletRequest request) {
        if (userUpdateDto == null || userUpdateDto.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = new User();
        //查询当前用户的信息
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        if (!loginUser.getId().equals(userUpdateDto.getId()) && !UserRoleEnum.ADMIN.equals(loginUser.getRole())) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR, "无权限");
        }
        BeanUtils.copyProperties(userUpdateDto, user);
        boolean result = userService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return Result.success("更新成功");
    }
    /**
     * 条件分页查询订单信息

     * @return
     */
    @Operation(summary = "条件分页查询订单信息")
    @GetMapping("/historyOrders")
    public Result<PageResult> page(int page, int pageSize, Integer status , String selectUser,HttpServletRequest request) {
        ThrowUtils.throwIf(page<=0 || pageSize<=0 , ErrorCode.PARAMS_ERROR , "页码出错");
        ThrowUtils.throwIf(StringUtils.isBlank(selectUser) , ErrorCode.PARAMS_ERROR , "selectUser不能为空");
        PageResult pageResult = orderService.userPage(page, pageSize, status ,selectUser, request);
        return Result.success(pageResult);
    }
    public static final String KEY = "SHOP_STATUS";
    @Resource
    private RedisTemplate redisTemplate;
    @GetMapping("/status")
    public Result<Integer> getStatus(){
        Integer status =  (Integer)redisTemplate.opsForValue().get(KEY);
        log.info("当前店铺状态为：{}", status == 1 ? "营业中" : "打烊中");
        return Result.success(status);
    }

}
